<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/27
 * Time: 11:26
 */

namespace App\HttpController\Home;


use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;

class Ad extends Base
{
    /**
     * banners
     * @return bool
     * @throws \EasySwoole\Component\Pool\Exception\PoolEmpty
     * @throws \EasySwoole\Component\Pool\Exception\PoolException
     * @throws \Throwable
     */
    public function lists()
    {
        $lists = MysqlPool::invoke(function (MysqlObject $db){
            $now = time();
            $lists = $db->where('switch',1)
                ->where('publishtime',$now,'<')
                ->where('endtime',$now,'>')
                ->get('siam_ad',null,'id,title,image,url,publishtime,endtime,position');
            if( $lists ){
                foreach ( $lists as $k=>$v ){
                    $lists[$k]['publishtime'] = date('Y-m-d H:i:s',$v['publishtime']);
                    $lists[$k]['endtime'] = date('Y-m-d H:i:s',$v['endtime']);
                }
            }
            return $lists;
        });
        return $this->writeJson(1,$lists,'请求成功');
    }

    public function position()
    {
        $position = $this->request()->getQueryParam('position');
        if( !$position ){
            return $this->writeJson(0,null,'缺少position参数');
        }
        $lists = MysqlPool::invoke(function (MysqlObject $db)use($position){
            $now = time();
            $lists = $db->where('switch',1)
                ->where('publishtime',$now,'<')
                ->where('endtime',$now,'>')
                ->where('position',$position)
                ->get('siam_ad',null,'id,title,image,url,publishtime,endtime,position');
            if( $lists ){
                foreach ( $lists as $k=>$v ){
                    $lists[$k]['publishtime'] = date('Y-m-d H:i:s',$v['publishtime']);
                    $lists[$k]['endtime'] = date('Y-m-d H:i:s',$v['endtime']);
                }
            }
            return $lists;
        });
        return $this->writeJson(1,$lists,'请求成功');
    }
}